<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="编号" prop="id" v-hasPermi="['admin']">
        <el-input
            v-model="queryParams.id"
            placeholder="请输入编号"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="代码" prop="stockCode">
        <el-input
            v-model="queryParams.stockCode"
            placeholder="请输入代码"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="名字" prop="stockName">
        <el-input
            v-model="queryParams.stockName"
            placeholder="请输入名字"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="清仓" prop="clearance">
        <el-select v-model="queryParams.clearance" placeholder="请选择清仓" clearable>
          <el-option
              v-for="dict in clearance"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
            type="primary"
            plain
            icon="Plus"
            @click="handleAdd"
            v-hasPermi="['enterprise:cost:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="success"
            plain
            icon="Edit"
            :disabled="single"
            @click="handleUpdate"
            v-hasPermi="['enterprise:cost:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="danger"
            plain
            icon="Delete"
            :disabled="multiple"
            @click="handleDelete"
            v-hasPermi="['enterprise:cost:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="warning"
            plain
            icon="Download"
            @click="handleExport"
            v-hasPermi="['enterprise:cost:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="costList" @selection-change="handleSelectionChange"
              size="small"
              element-loading-background="#283554">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="编号" align="center" prop="id" min-width="5%"/>
      <!--      <el-table-column label="用户 id" align="center" prop="userId" min-width="5%"/>-->
      <el-table-column label="代码" align="center" prop="stockCode" min-width="5%">
        <template #default="scope">
          <el-text tag="b">{{ scope.row.stockCode }}</el-text>
        </template>
      </el-table-column>
      <el-table-column label="名字" align="center" prop="stockName" min-width="5%">
        <template #default="scope">
          <el-text tag="b">{{ scope.row.stockName }}</el-text>
        </template>
      </el-table-column>

      <el-table-column label="股价(元)" align="center" prop="stockPriceCurrent" min-width="5%">
        <template #default="scope">
          <el-text tag='b' class="mx-1" type="danger">{{ scope.row.stockPriceCurrent }}</el-text>
        </template>
      </el-table-column>
      <el-table-column label="成本(元)" align="center" prop="stockPriceCost" min-width="5%">
        <template #default="scope">
          <el-text tag="b" class="mx-1" type="warning">{{ scope.row.stockPriceCost }}</el-text>
        </template>
      </el-table-column>
      <el-table-column label="持仓(股)" align="center" prop="stockCount" min-width="5%">
        <template #default="scope">
          <el-text tag="b" class="mx-1" type="primary">{{ scope.row.stockCount }}</el-text>
        </template>
      </el-table-column>
      <el-table-column label="持仓盈亏(元)" align="center" prop="gainAndLoss" min-width="5%">
        <template #default="scope">
          <el-text tag="b" class="mx-1" :type="handleYK(scope.row.gainAndLoss)">{{
              scope.row.gainAndLoss
            }}
          </el-text>
        </template>
      </el-table-column>
      <el-table-column label="增减数量(股)" align="center" prop="stockCountChangeAmount" min-width="5%"/>
      <el-table-column label="增持持仓(股)" align="center" prop="stockCountAfterBuy" min-width="5%">
        <template #default="scope">
          <el-text class="mx-1" type="primary">{{ scope.row.stockCountAfterBuy }}</el-text>
        </template>
      </el-table-column>
      <el-table-column label="减持持仓(股)" align="center" prop="stockCountAfterSell" min-width="5%">
        <template #default="scope">
          <el-text class="mx-1" type="primary">{{ scope.row.stockCountAfterSell }}</el-text>
        </template>
      </el-table-column>
      <el-table-column label="增持金额(元)" align="center" prop="stockBuyAmount" min-width="5%">
        <template #default="scope">
          <el-text class="mx-1" type="success">{{ scope.row.stockBuyAmount }}</el-text>
        </template>
      </el-table-column>
      <el-table-column label="减持金额(元)" align="center" prop="stockSellAmount" min-width="5%">
        <template #default="scope">
          <el-text class="mx-1" type="success">{{ scope.row.stockSellAmount }}</el-text>
        </template>
      </el-table-column>
      <el-table-column label="增持成本(元)" align="center" prop="stockPriceCostAfterBuy" min-width="5%">
        <template #default="scope">
          <el-text class="mx-1" type="warning">{{ scope.row.stockPriceCostAfterBuy }}</el-text>
        </template>
      </el-table-column>
      <el-table-column label="减持成本(元)" align="center" prop="stockPriceCostAfterSell" min-width="5%">
        <template #default="scope">
          <el-text class="mx-1" type="warning">{{ scope.row.stockPriceCostAfterSell }}</el-text>
        </template>
      </el-table-column>

      <el-table-column label="清仓" align="center" prop="clearance" min-width="5%">
        <template #default="scope">
          <dict-tag :options="clearance" :value="scope.row.clearance"/>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="note" min-width="6%">
        <template #default="scope">
          <el-text size="small" class="mx-1" type="info" line-clamp="2">{{ scope.row.note }}</el-text>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" min-width="7%" v-if="false">
        <template #default="scope">
          <el-text class="mx-1" type="info" line-clamp="2" size="small">
            <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span></el-text>
        </template>
      </el-table-column>
      <el-table-column label="更新时间" align="center" prop="updateTime" min-width="7%" v-if="false">
        <template #default="scope">
          <el-text class="mx-1" type="info" line-clamp="2" size="small">
            <span>{{ parseTime(scope.row.updateTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span></el-text>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" min-width="10%">
        <template #default="scope">
          <el-button size="small" link type="primary" icon="Edit" @click="handleUpdate(scope.row)"
                     v-hasPermi="['enterprise:cost:edit']">修改
          </el-button>
          <el-button size="small" link type="primary" icon="Delete" @click="handleDelete(scope.row)"
                     v-hasPermi="['enterprise:cost:remove']">删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
        v-show="total>0"
        :total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
    />

    <!-- 添加或修改本金计算对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="costRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="代码" prop="stockCode">
          <el-input v-model="form.stockCode" placeholder="请输入代码"/>
        </el-form-item>
        <el-form-item label="名字" prop="stockName">
          <el-input v-model="form.stockName" placeholder="请输入名字"/>
        </el-form-item>
        <el-form-item label="持仓成本" prop="stockPriceCost">
          <el-input v-model="form.stockPriceCost" placeholder="请输入持仓成本"/>
        </el-form-item>
        <el-form-item label="当前股价" prop="stockPriceCurrent">
          <el-input v-model="form.stockPriceCurrent" placeholder="请输入当前股价"/>
        </el-form-item>
        <el-form-item label="持仓股数" prop="stockCount">
          <el-input v-model="form.stockCount" placeholder="请输入持仓股数"/>
        </el-form-item>
        <el-form-item label="变动数量" prop="stockCountChangeAmount">
          <el-input v-model="form.stockCountChangeAmount" placeholder="请输入变动数量"/>
        </el-form-item>
        <el-form-item label="盈亏" prop="gainAndLoss">
          <el-input v-model="form.gainAndLoss" placeholder="请输入盈亏"/>
        </el-form-item>
        <el-form-item label="清仓" prop="clearance">
          <el-select v-model="form.clearance" placeholder="请选择清仓">
            <el-option
                v-for="dict in clearance"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="备注" prop="note">
          <el-input v-model="form.note" type="textarea" placeholder="请输入内容"/>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Cost">
import {listCost, getCost, delCost, addCost, updateCost} from "@/api/enterprise/cost";

const {proxy} = getCurrentInstance();
const {clearance} = proxy.useDict('clearance');

const costList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    stockCode: null,
    stockName: null,
    clearance: null,
    note: null
  },
  rules: {
    stockCode: [
      {required: true, message: "代码不能为空", trigger: "blur"}
    ],
    stockPriceCost: [
      {required: true, message: "持仓成本不能为空", trigger: "blur"}
    ],
    stockPriceCurrent: [
      {required: true, message: "当前股价不能为空", trigger: "blur"}
    ],
    stockCount: [
      {required: true, message: "持仓股数不能为空", trigger: "blur"}
    ],
    stockCountChangeAmount: [
      {required: true, message: "变动数量不能为空", trigger: "blur"}
    ],
  }
});

const {queryParams, form, rules} = toRefs(data);

/** 查询本金计算列表 */
function getList() {
  loading.value = true;
  listCost(queryParams.value).then(response => {
    costList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}


// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    createTime: null,
    updateTime: null,
    userId: null,
    stockCode: null,
    stockName: null,
    stockPriceCost: null,
    stockPriceCurrent: null,
    stockCount: null,
    stockCountChangeAmount: null,
    stockCountAfterBuy: null,
    stockCountAfterSell: null,
    stockBuyAmount: null,
    stockSellAmount: null,
    stockPriceCostAfterBuy: null,
    stockPriceCostAfterSell: null,
    gainAndLoss: null,
    clearance: null,
    note: null
  };
  proxy.resetForm("costRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加本金计算";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value
  getCost(_id).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改本金计算";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["costRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateCost(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addCost(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除本金计算编号为"' + _ids + '"的数据项？').then(function () {
    return delCost(_ids);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {
  });
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('enterprise/cost/export', {
    ...queryParams.value
  }, `cost_${new Date().getTime()}.xlsx`)
}

getList();
</script>
